<?php

declare(strict_types=1);

namespace Dreamcat\PropertyAnalysis\Pojo;

use Dreamcat\PropertyAnalysis\Utils\BuildinTypeHelper;
use JetBrains\PhpStorm\Immutable;
use JetBrains\PhpStorm\Pure;
use ReflectionClass;

/**
 * 一种可能的类型描述
 * @author vijay
 */
class OneMayTypeDesc
{
    /** @var TypeDescription|null 元素类型 */
    private ?TypeDescription $arrayElement = null;

    /** @var bool|null 是否map */
    private ?bool $isMap = null;

    /**
     * @param string|ReflectionClass $propertyType 属性类型
     */
    public function __construct(#[Immutable] private string|ReflectionClass $propertyType)
    {
    }

    /**
     * @return bool
     */
    #[Pure]
    public function isClass(): bool
    {
        return $this->isClassRef() && !$this->propertyType->isInterface();
    }

    /**
     * @return bool
     */
    #[Pure]
    public function isClassRef(): bool
    {
        return $this->propertyType instanceof ReflectionClass;
    }

    /**
     * @return bool
     */
    #[Pure]
    public function isScalar(): bool
    {
        return !$this->isClassRef() && BuildinTypeHelper::IS_SCALAR[$this->propertyType];
    }

    /**
     * @return bool
     */
    #[Pure]
    public function isInterface(): bool
    {
        return $this->isClassRef() && $this->propertyType->isInterface();
    }

    /**
     * @return TypeDescription|null
     */
    #[Pure]
    public function getArrayElement(): ?TypeDescription
    {
        return $this->arrayElement;
    }

    /**
     * @param TypeDescription|null $arrayElement
     */
    public function setArrayElement(?TypeDescription $arrayElement): void
    {
        $this->arrayElement = $arrayElement;
    }

    /**
     * @return ReflectionClass|string
     */
    #[Pure]
    public function getPropertyType(): ReflectionClass|string
    {
        return $this->propertyType;
    }

    /**
     * @return bool|null 是否map
     */
    #[Pure]
    public function getIsMap(): ?bool
    {
        return $this->isMap;
    }

    /**
     * @param bool $isMap 是否map
     * @return OneMayTypeDesc
     */
    public function setIsMap(bool $isMap): OneMayTypeDesc
    {
        $this->isMap = $isMap;
        return $this;
    }
}

# end of file
